package service;

import java.util.ArrayList;
import java.util.Iterator;
import adminDao.AdminDao;
import animal.AnimalItem;

public class AdminService {
	private AdminDao adminDao=new AdminDao();
	public ArrayList<AnimalItem>queryAnimalItem(){
		ArrayList<AnimalItem>data=adminDao.queryAllData();
		return data;
	}
	public boolean addAnimal(String number,String sort,String age,String amount,String price){
		ArrayList<AnimalItem>data=queryAnimalItem();
		Iterator<AnimalItem>itertor=data.iterator();
		while(itertor.hasNext()){
			AnimalItem animal=itertor.next();
			if(animal.getNumber().equals(number)){
				return false;
			}
		}
		AnimalItem thisAniaml=new AnimalItem(number,sort,Double.parseDouble(age),amount,Double.parseDouble(price));
		adminDao.addAnimalItem(thisAniaml);
		return true;
	}
	public boolean updateAnimalItem(String number,String sort,String age,String amount,String price){
		ArrayList<AnimalItem>data=queryAnimalItem();
		for(int i=0;i<data.size();i++){
			AnimalItem animal=data.get(i);
			if(animal.getNumber().equals(number)){
				adminDao.delAnimalItem(number);
				AnimalItem thisAniaml=new AnimalItem(number,sort,Double.parseDouble(age),amount,Double.parseDouble(price));
				adminDao.addAnimalItem(thisAniaml);
				return true;
			}
		}
		return false;
	}
	public boolean delAnimalItem(String number){
		ArrayList<AnimalItem>data=queryAnimalItem();
		for(int i=0;i<data.size();i++){
			AnimalItem animal=data.get(i);
			if(animal.getNumber().equals(number)){
				adminDao.delAnimalItem(number);
				return true;
			}
		}
		return false;
	}
}
